package View;

import java.awt.Color;
import java.awt.Font;
import java.awt.SystemColor;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import Bussiness.Search_Action;
import Bussiness.Update_Action;
import Model.KhoaHoc;
import Model.LopHoc;

/**
 * 
 * @author Nhóm java lớp PNV2T - Nguyễn Thị Lệ Đài - Nguyễn Chí Hướng
 * 
 */
public class SuaLopHoc extends javax.swing.JFrame {
    
    Vector vecKhoaHoc = null;
    LopHoc lop = null;
    /** Creates new form SuaLopHoc */
    public SuaLopHoc(int ID) {
    	setAlwaysOnTop(true);
    	setResizable(false);
    	setIconImage(Toolkit.getDefaultToolkit().getImage(SuaLopHoc.class.getResource("/Icon/QLSV.jpg")));
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
        } catch (InstantiationException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        initComponents();
        setLocation(520, 50);
        AddComBoBox();
        Search_Action sa = new Search_Action();
        lop = sa.getLop(ID);
        txt_TenLop.setText(lop.getTenLop());
        txt_ChuNhiem.setText(lop.getChuNhiem());
        txt_LopTruong.setText(lop.getLopTruong());
        sa.Close();
    }
    
    public void AddComBoBox(){
        Search_Action sa = new Search_Action();
        vecKhoaHoc = sa.DanhSachKhoaHoc();
        cbb_KhoaHoc.removeAllItems();
        cbb_KhoaHoc.addItem("(--- Chọn khóa học ---)");
        for (int i=0;i<vecKhoaHoc.size();i++){
            KhoaHoc kh = (KhoaHoc)vecKhoaHoc.get(i);
            cbb_KhoaHoc.addItem(kh.getTenKhoa());
        }
        sa.Close();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        jToolBar1 = new javax.swing.JToolBar();
        btn_CapNhap = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        cbb_HeDaoTao = new javax.swing.JComboBox();
        txt_TenLop = new javax.swing.JTextField();
        cbb_KhoaHoc = new javax.swing.JComboBox();
        txt_ChuNhiem = new javax.swing.JTextField();
        txt_LopTruong = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("S\u1eeda th\u00f4ng tin L\u1edbp h\u1ecdc");
        jPanel1.setBackground(SystemColor.activeCaption);
        jToolBar1.setBackground(SystemColor.inactiveCaption);
        jToolBar1.setFloatable(false);

        btn_CapNhap.setBackground(new java.awt.Color(227, 239, 249));
        btn_CapNhap.setFont(new java.awt.Font("Times New Roman", 1, 13));
        btn_CapNhap.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Icon/Sua.png")));
        btn_CapNhap.setText("C\u1eadp Nh\u1eadp");
        btn_CapNhap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btn_CapNhapActionPerformed(evt);
            }
        });

        jToolBar1.add(btn_CapNhap);

        jLabel3.setFont(new java.awt.Font("Times New Roman", 0, 13));
        jLabel3.setText("T\u00ean L\u1edbp :");

        jLabel4.setFont(new java.awt.Font("Times New Roman", 0, 13));
        jLabel4.setText("Kh\u00f3a H\u1ecdc :");

        jLabel5.setFont(new java.awt.Font("Times New Roman", 0, 13));
        jLabel5.setText("H\u1ec7 \u0110\u00e0o T\u1ea1o :");

        jLabel6.setFont(new java.awt.Font("Times New Roman", 0, 13));
        jLabel6.setText("Ch\u1ee7 Nhi\u1ec7m :");

        jLabel7.setFont(new java.awt.Font("Times New Roman", 0, 13));
        jLabel7.setText("L\u1edbp Tr\u01b0\u1edfng :");

        cbb_HeDaoTao.setFont(new java.awt.Font("Times New Roman", 0, 13));
        cbb_HeDaoTao.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "(--- Chọn hệ đào tạo ---)", "\u0110\u1ea1i H\u1ecdc Ch\u00ednh Qui", "Cao \u0110\u1eb3ng", "T\u1ea1i Ch\u1ee9c" }));

        txt_TenLop.setFont(new java.awt.Font("Times New Roman", 0, 13));
        txt_TenLop.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));

        cbb_KhoaHoc.setFont(new java.awt.Font("Times New Roman", 0, 13));

        txt_ChuNhiem.setFont(new java.awt.Font("Times New Roman", 0, 13));
        txt_ChuNhiem.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));

        txt_LopTruong.setFont(new java.awt.Font("Times New Roman", 0, 13));
        txt_LopTruong.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        
        label = new JLabel();
        label.setText("(*)");
        label.setForeground(new Color(204, 0, 0));
        label.setFont(new Font("Times New Roman", Font.PLAIN, 13));
        
        label_1 = new JLabel();
        label_1.setText("(*)");
        label_1.setForeground(new Color(204, 0, 0));
        label_1.setFont(new Font("Times New Roman", Font.PLAIN, 13));
        
        label_2 = new JLabel();
        label_2.setText("(*)");
        label_2.setForeground(new Color(204, 0, 0));
        label_2.setFont(new Font("Times New Roman", Font.PLAIN, 13));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1Layout.setHorizontalGroup(
        	jPanel1Layout.createParallelGroup(Alignment.LEADING)
        		.addGroup(jPanel1Layout.createSequentialGroup()
        			.addContainerGap()
        			.addGroup(jPanel1Layout.createParallelGroup(Alignment.LEADING)
        				.addGroup(jPanel1Layout.createSequentialGroup()
        					.addComponent(jLabel7)
        					.addPreferredGap(ComponentPlacement.RELATED)
        					.addComponent(txt_LopTruong, GroupLayout.PREFERRED_SIZE, 191, GroupLayout.PREFERRED_SIZE))
        				.addGroup(jPanel1Layout.createSequentialGroup()
        					.addGroup(jPanel1Layout.createParallelGroup(Alignment.LEADING)
        						.addComponent(jLabel5)
        						.addComponent(jLabel3)
        						.addComponent(jLabel4)
        						.addComponent(jLabel6))
        					.addPreferredGap(ComponentPlacement.RELATED)
        					.addGroup(jPanel1Layout.createParallelGroup(Alignment.LEADING, false)
        						.addComponent(txt_ChuNhiem, GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
        						.addComponent(txt_TenLop, GroupLayout.DEFAULT_SIZE, 191, Short.MAX_VALUE)
        						.addComponent(cbb_KhoaHoc, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        						.addComponent(cbb_HeDaoTao, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        					.addPreferredGap(ComponentPlacement.RELATED)
        					.addGroup(jPanel1Layout.createParallelGroup(Alignment.LEADING)
        						.addComponent(label_2, GroupLayout.PREFERRED_SIZE, 15, GroupLayout.PREFERRED_SIZE)
        						.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 15, GroupLayout.PREFERRED_SIZE)
        						.addComponent(label, GroupLayout.PREFERRED_SIZE, 15, GroupLayout.PREFERRED_SIZE))))
        			.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        		.addComponent(jSeparator1, GroupLayout.DEFAULT_SIZE, 304, Short.MAX_VALUE)
        		.addComponent(jToolBar1, GroupLayout.DEFAULT_SIZE, 304, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
        	jPanel1Layout.createParallelGroup(Alignment.LEADING)
        		.addGroup(jPanel1Layout.createSequentialGroup()
        			.addComponent(jToolBar1, GroupLayout.PREFERRED_SIZE, 38, GroupLayout.PREFERRED_SIZE)
        			.addGap(8)
        			.addComponent(jSeparator1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
        			.addGap(29)
        			.addGroup(jPanel1Layout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(jLabel3)
        				.addComponent(txt_TenLop, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
        				.addComponent(label, GroupLayout.PREFERRED_SIZE, 16, GroupLayout.PREFERRED_SIZE))
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addGroup(jPanel1Layout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(jLabel4)
        				.addComponent(cbb_KhoaHoc, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
        				.addComponent(label_1, GroupLayout.PREFERRED_SIZE, 16, GroupLayout.PREFERRED_SIZE))
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addGroup(jPanel1Layout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(jLabel5)
        				.addComponent(cbb_HeDaoTao, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE)
        				.addComponent(label_2, GroupLayout.PREFERRED_SIZE, 16, GroupLayout.PREFERRED_SIZE))
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addGroup(jPanel1Layout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(jLabel6)
        				.addComponent(txt_ChuNhiem, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE))
        			.addPreferredGap(ComponentPlacement.RELATED)
        			.addGroup(jPanel1Layout.createParallelGroup(Alignment.BASELINE)
        				.addComponent(jLabel7)
        				.addComponent(txt_LopTruong, GroupLayout.PREFERRED_SIZE, 24, GroupLayout.PREFERRED_SIZE))
        			.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1.setLayout(jPanel1Layout);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        pack();
  }// </editor-fold>//GEN-END:initComponents

    
    private void btn_CapNhapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btn_CapNhapActionPerformed
        String TenLop = txt_TenLop.getText();
        int row = cbb_KhoaHoc.getSelectedIndex();
        int index = cbb_HeDaoTao.getSelectedIndex();
        String ChuNhiem = txt_ChuNhiem.getText();
        String LopTruong = txt_LopTruong.getText();
        
        if (TenLop.equalsIgnoreCase("")){
            JOptionPane.showMessageDialog(null,"B\u1ea1n ch\u01b0a nh\u1eadp T\u00ean L\u1edbp !","Null Values !",JOptionPane.ERROR_MESSAGE);
            txt_TenLop.requestFocus();
        } else if (row==0){
            JOptionPane.showMessageDialog(null,"B\u1ea1n ch\u01b0a ch\u1ecdn Kh\u00f3a H\u1ecdc !","Null Values !",JOptionPane.ERROR_MESSAGE);
        } else if (index==0){
            JOptionPane.showMessageDialog(null,"B\u1ea1n ch\u01b0a ch\u1ecdn H\u1ec7 \u0110\u00e0o T\u1ea1o !","Null Values !",JOptionPane.ERROR_MESSAGE);
        } else {
            lop.setTenLop(TenLop);
            KhoaHoc kh = (KhoaHoc)vecKhoaHoc.get(row-1);
            lop.setMaKhoa(kh.getID());
            if(index==1)
                lop.setHeDaoTao("DH");
            else if(index==2)
                lop.setHeDaoTao("CD");
            else if(index==3)
                lop.setHeDaoTao("TC");
            lop.setChuNhiem(ChuNhiem);
            lop.setLopTruong(LopTruong);
            
            Update_Action ua = new Update_Action();
            if (ua.SuaLopHoc(lop)==true){
                int j= JOptionPane.showConfirmDialog(null,"Sửa thành công,Bạn có muốn tiếp tục ?","Continued ... !",2);
                if (j!=0)
                    this.dispose();
                else{
                }
            } else {
                JOptionPane.showMessageDialog(this,"Không thể sửa được !");
            }
        }
    }//GEN-LAST:event_btn_CapNhapActionPerformed
    
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btn_CapNhap;
    private javax.swing.JComboBox cbb_HeDaoTao;
    private javax.swing.JComboBox cbb_KhoaHoc;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JTextField txt_ChuNhiem;
    private javax.swing.JTextField txt_LopTruong;
    private javax.swing.JTextField txt_TenLop;
    private JLabel label;
    private JLabel label_1;
    private JLabel label_2;
    // End of variables declaration//GEN-END:variables
    
}
